local Msg = require("core.Msg")
local BaowuExcel = require("excel.Baowu")
local BaowuConfig = BaowuExcel.baowu
local ObjHuman = require("core.ObjHuman")
local RoleAttr = require("role.RoleAttr")
local SkillNotify  = require("skill.Notify")
local Grid = require("bag.Grid")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local BagLogic = require("bag.BagLogic")
local common = BaowuExcel.common
local Status = require("scene.Status")
local CoolDown = require("skill.CoolDown")
local RankDefine = require("rank.RankDefine")
local YYDaBiao = require("activity.operate.DaBiao")
local AcvityDefine = require("activity.Define")
local ItemDefine = require("bag.ItemDefine")
local OfficialLogic = require("official.OfficialLogic")

--计算单个宝物属性
function calcAttr(human, id, attrs, isNext)
    local data = ObjHuman.getBaowu(human, id)
    RoleAttr.addAttrs(attrs, BaowuConfig[id].addAttr)
    local lv = data and data.lv or 0
    local starLv = data and data.starLv or 0
    local quality = math.floor(starLv / 8) == 0 and 0 or math.floor(starLv / 8)
    local BaowuLvConfig = BaowuExcel[BaowuConfig[id].name.."Lv"]
    if isNext and BaowuLvConfig[lv + 1] then
        RoleAttr.addAttrs(attrs, BaowuLvConfig[lv + 1].addAttr)
    elseif lv ~= 0 then
        RoleAttr.addAttrs(attrs, BaowuLvConfig[lv].addAttr)
    end
    local BaowuStarConfig = BaowuExcel[BaowuConfig[id].name.."Star"]
    if quality ~= 0 then
        for i=1, quality + 1 do
            if i <= quality then
                RoleAttr.addAttrs(attrs, BaowuStarConfig[i].addQuaAttr)
            end
            for j=1, 8 do
                if (i - 1) * 8 + j <= starLv then
                    RoleAttr.addOneAttr(attrs, BaowuStarConfig[i].addAttr[j])
                end
            end
        end
    else
        for j=1, 8 do
            if j <= starLv then
                RoleAttr.addOneAttr(attrs, BaowuStarConfig[1].addAttr[j])
            end
        end
    end
    if data and quality > 0 then
      RoleAttr.addAttrs(attrs, BaowuExcel[BaowuConfig[id].name.."LH"][data.LonghunLv].addQuaAttr)
    end
end
--设置行星星tip信息
function setTipsInfo(human, id, tips)
    local BaowuStarConfig = BaowuExcel[BaowuConfig[id].name.."Star"]
    local data = ObjHuman.getBaowu(human, id)
    local quality = math.floor(data.starLv / 8) + 1
    if quality >= #BaowuStarConfig then
        quality = quality - 1
    end
    local cache = {}
    tips[0] = 0
    for i=1, 8 do
        tips[i].attr[0] = 0
        RoleAttr.addOneAttr(tips[i].attr, BaowuStarConfig[quality].addAttr[i])
        if i == 8 then
            RoleAttr.addAttrs(tips[i].attr, BaowuStarConfig[quality].addQuaAttr)
        end
        tips[i].needYinliang = BaowuStarConfig[quality].addAttr[i][4]
        tips[i].needItem[0] = 1
        Grid.create(cache, BaowuConfig[id].needItemId, BaowuStarConfig[quality].addAttr[i][3])
        Grid.makeItem(cache, tips[i].needItem[1], 0, human)
        tips[0] = tips[0] + 1
    end
end
--设置宝物信息
function setBaowuInfo(human, id, info, isOther)
    local data = ObjHuman.getBaowu(human, id)
    info.id = id
    info.name = BaowuConfig[id].mingzi
    info.attr[0] = 0
    local cache = {}
    calcAttr(human, id, info.attr)
    info.power = RoleAttr.calPowerProto(info.attr) + getBaowuSkillPower(human, id)
    SkillNotify.setSkillData(human, BaowuConfig[id].skillId, info.skill, data and data.status ~= 0 and data.skillLv or 0, nil, isOther)
    SkillNotify.setSkillData(human, data and data.skillId ~= 0 and data.skillId or BaowuConfig[id].skillId, info.useSkill, data and data.status ~= 0 and data.skillLv or 0, nil, isOther)
    info.starIsFull = 0
    info.image = BaowuConfig[id].image
    info.taotalExp = BaowuExcel[BaowuConfig[id].name.."Lv"][data and data.lv + 1 or 1].exp
    if data then
        if isOther then
            info.remainTime = 0
        else
            info.remainTime = (data.limitTime - (os.time() - data.startTime) > 0) and data.limitTime - (os.time() - data.startTime) or 0
        end 
        info.status = data.status
        info.lv = data.lv
        info.lhLv = data.LonghunLv
        local BaowuLvConfig = BaowuExcel[BaowuConfig[id].name.."Lv"]
        if data.lv >= #BaowuLvConfig then
            info.exp = 0
        else
            info.exp = data.exp
        end
        info.star = data.starLv
        if data.starLv / 8 >= #BaowuExcel[BaowuConfig[id].name.."Star"] then
            info.starIsFull = 1
        end
        setTipsInfo(human, id, info.tips)
        info.nextLvAttr[0] = 0
        if BaowuLvConfig[data.lv + 1] then
            for i=1, #BaowuLvConfig[data.lv + 1].addAttr do
                local temp = {}
                temp[1] = BaowuLvConfig[data.lv + 1].addAttr[i][1]
                if data.lv ~= 0 then
                    temp[2] = BaowuLvConfig[data.lv + 1].addAttr[i][2] - BaowuLvConfig[data.lv].addAttr[i][2]
                else
                    temp[2] = BaowuLvConfig[data.lv + 1].addAttr[i][2]
                end
                RoleAttr.addOneAttr(info.nextLvAttr, temp)
            end
            info.nextLvPower = RoleAttr.calPowerProto(info.nextLvAttr)
        else
            info.nextLvAttr[0] = 0
            info.nextLvPower = 0
        end
        local BaowuLHConfig = BaowuExcel[BaowuConfig[id].name.."LH"]
        info.jihuoNeed[0] = 1
        Grid.create(cache, BaowuConfig[id].needItemId, BaowuConfig[id].needItemCnt)
        Grid.makeItem(cache, info.jihuoNeed[1], 0, human)
        data.power = info.power
        info.lhLeftIsUse = 0
        info.lhRightIsUse = 0
        if BaowuConfig[id].leftSkill[1][1] == data.leftSkillId or BaowuConfig[id].leftSkill[2][1] == data.leftSkillId then
            info.lhLeftIsUse = 1
        end
        if BaowuConfig[id].rightSkill[1][1] == data.rightSkillId or BaowuConfig[id].rightSkill[2][1] == data.rightSkillId then
            info.lhRightIsUse = 1
        end        
        info.leftSkill[0] = 2
        SkillNotify.setSkillData(human, BaowuConfig[id].leftSkill[1][1], info.leftSkill[1], data and data.status ~= 0 and data.skillLv or 0, nil, isOther)
        SkillNotify.setSkillData(human, BaowuConfig[id].leftSkill[2][1], info.leftSkill[2], data and data.status ~= 0 and data.skillLv or 0, nil, isOther)
        info.rightSkill[0] = 2
        SkillNotify.setSkillData(human, BaowuConfig[id].rightSkill[1][1], info.rightSkill[1], data and data.status ~= 0 and data.skillLv or 0, nil, isOther)
        SkillNotify.setSkillData(human, BaowuConfig[id].rightSkill[2][1], info.rightSkill[2], data and data.status ~= 0 and data.skillLv or 0, nil, isOther)
        if data.LonghunLv >= 0 and data.LonghunLv < #BaowuLHConfig then
            info.lhStageNeedYL = BaowuLHConfig[data.LonghunLv + 1].needYinliang
            info.lhStageNeedItem[0] = #BaowuLHConfig[data.LonghunLv + 1].needItem
            for i=1, #BaowuLHConfig[data.LonghunLv + 1].needItem do    
                Grid.create(cache, BaowuLHConfig[data.LonghunLv + 1].needItem[i][1], BaowuLHConfig[data.LonghunLv + 1].needItem[i][2])
                Grid.makeItem(cache, info.lhStageNeedItem[i], 0, human)
            end
        else
            info.lhStageNeedItem[0] = 0
            info.lhStageNeedYL = 0
        end
        info.lhAddAttr[0] = 0
        RoleAttr.addAttrs(info.lhAddAttr, BaowuLHConfig[data.LonghunLv].addQuaAttr)
        info.lhAddPower = RoleAttr.calPowerProto(info.lhAddAttr)
        info.lhNextAddAttr[0] = 0
        if BaowuLHConfig[data.LonghunLv + 1] then
            for i=1, #BaowuLHConfig[data.LonghunLv + 1].addQuaAttr do
                local temp = {}
                temp[1] = BaowuLHConfig[data.LonghunLv + 1].addQuaAttr[i][1]
                temp[2] = BaowuLHConfig[data.LonghunLv + 1].addQuaAttr[i][2] - BaowuLHConfig[data.LonghunLv].addQuaAttr[i][2]
                RoleAttr.addOneAttr(info.lhNextAddAttr, temp)
            end
            info.lhNextPower = RoleAttr.calPowerProto(info.lhNextAddAttr)
        else
            info.lhNextAddAttr[0] = 0
            info.lhNextPower = 0
        end
        info.lhLeftNeedLv = 0
        info.lhRightNeedLv = 0
        info.lhLeftJJNeedYL = 0
        info.lhRightJJNeedYL = 0
        info.lhLeftJJNeedItem[0] = 0
        info.lhRightJJNeedItem[0] = 0
        info.lhLeftDetail = BaowuConfig[id].leftSkill[2][4]
        info.lhRightDetail = BaowuConfig[id].rightSkill[2][4]
        local cache = {}
        if 0 == data.leftSkillId  then
            info.lhLeftNeedLv = BaowuConfig[id].leftSkill[1][2]
            info.lhLeftJJNeedYL = BaowuConfig[id].leftSkill[1][3]            
            info.lhLeftJJNeedItem[0] = #BaowuConfig[id].leftNeedItem[1]
            for i=1, #BaowuConfig[id].leftNeedItem[1] do
              Grid.create(cache, BaowuConfig[id].leftNeedItem[1][i][1], BaowuConfig[id].leftNeedItem[1][i][2])
              Grid.makeItem(cache, info.lhLeftJJNeedItem[i], 0, human)
            end
            info.lhLeftStatus = 0 
            info.lhLeftDetail = BaowuConfig[id].leftSkill[1][4]           
        elseif BaowuConfig[id].leftSkill[1][1] == data.leftSkillId then
            info.lhLeftNeedLv = BaowuConfig[id].leftSkill[2][2]
            info.lhLeftJJNeedYL = BaowuConfig[id].leftSkill[2][3]
            info.lhLeftJJNeedItem[0] = #BaowuConfig[id].leftNeedItem[2]
            for i=1, #BaowuConfig[id].leftNeedItem[1] do
              Grid.create(cache, BaowuConfig[id].leftNeedItem[2][i][1], BaowuConfig[id].leftNeedItem[2][i][2])
              Grid.makeItem(cache, info.lhLeftJJNeedItem[i], 0, human)
            end
            info.lhLeftStatus = 1
        elseif BaowuConfig[id].leftSkill[2][1] == data.leftSkillId then
            info.lhLeftStatus = 2
        end
        if 0 == data.rightSkillId then
            info.lhRightJJNeedItem[0] = #BaowuConfig[id].rightNeedItem[1]
            for i=1, #BaowuConfig[id].rightNeedItem[1] do
              Grid.create(cache, BaowuConfig[id].rightNeedItem[1][i][1], BaowuConfig[id].rightNeedItem[1][i][2])
              Grid.makeItem(cache, info.lhRightJJNeedItem[i], 0, human)
            end
            info.lhRightStatus = 0
            info.lhRightNeedLv = BaowuConfig[id].rightSkill[1][2]
            info.lhRightJJNeedYL = BaowuConfig[id].rightSkill[1][3]
            info.lhRightDetail = BaowuConfig[id].rightSkill[1][4]
        elseif BaowuConfig[id].rightSkill[1][1] == data.rightSkillId then
            info.lhRightNeedLv = BaowuConfig[id].rightSkill[2][2]
            info.lhRightJJNeedYL = BaowuConfig[id].rightSkill[2][3]
            info.lhRightJJNeedItem[0] = #BaowuConfig[id].rightNeedItem[2]
            for i=1, #BaowuConfig[id].rightNeedItem[2] do
              Grid.create(cache, BaowuConfig[id].rightNeedItem[2][i][1], BaowuConfig[id].rightNeedItem[2][i][2])
              Grid.makeItem(cache, info.lhRightJJNeedItem[i], 0, human)
            end
            info.lhRightStatus = 1
        elseif BaowuConfig[id].rightSkill[2][1] == data.rightSkillId then
            info.lhRightStatus = 2
        end
        info.lhJihuoNeedQua = BaowuConfig[id].lhNeedQua
    else
        info.remainTime = 0
        info.lhLv = 0
        info.status = 0
        info.lv = 0
        info.exp = 0
        info.star = 0
        info.nextLvAttr[0] = 0
        info.nextLvPower = 0
        info.tips[0] = 0
        info.leftSkill[0] = 0
        info.rightSkill[0] = 0
        info.lhLeftIsUse = 0
        info.lhRightIsUse = 0
        info.lhStageNeedYL = 0
        info.lhStageNeedItem[0] = 0
        info.lhAddAttr[0] = 0     
        info.lhLeftNeedLv = 0
        info.lhRightNeedLv = 0   
        info.lhNextAddAttr[0] = 0
        info.lhNextPower = 0
        info.lhJihuoNeedQua = 0
        info.lhLeftStatus = 0
        info.lhRightStatus = 0
        info.lhLeftJJNeedYL = 0
        info.lhRightJJNeedYL = 0
        info.lhLeftJJNeedItem[0] = 0
        info.lhRightJJNeedItem[0] = 0
        info.lhAddPower = 0
        info.lhLeftDetail = ""
        info.lhRightDetail = ""
        info.jihuoNeed[0] = 1
        Grid.create(cache, BaowuConfig[id].needItemId, BaowuConfig[id].needItemCnt)
        Grid.makeItem(cache, info.jihuoNeed[1], 0, human)
    end
    YYDaBiao.checkCanGet(human, AcvityDefine.OPERATEACT_TYPE_BAOWU)
end
--计算角色属性
function doCalc(human)
    local sendMsg = Msg.gc.GC_UPDATE_BAOWU
    sendMsg.allAttr[0] = 0
    getAllAttr(human, sendMsg.allAttr)
    for i=1, sendMsg.allAttr[0] do
        human.attr[sendMsg.allAttr[i].key] = human.attr[sendMsg.allAttr[i].key] + sendMsg.allAttr[i].value
    end
end
--宝物总属性
function getAllAttr(human, attrs)
    for i=0, human.db.baowuLen - 1 do
      if human.db.baowu[i].status ~= 0 then
        calcAttr(human, human.db.baowu[i].id, attrs)
      end
    end
end
--得到宝物技能战斗力
function getBaowuSkillPower(human, id)
    local skillPower = 0
    local data = ObjHuman.getBaowu(human, human.db.combatBaowu)
    if data then
      if id == human.db.combatBaowu or not id then
        skillPower = data.skillLv * 30
      end
    end
    return skillPower
end
--更新宝物
function UpdateBaowu(human, id, isPowerChange)
    local sendMsg = Msg.gc.GC_UPDATE_BAOWU
    setBaowuInfo(human, id, sendMsg.info)
    sendMsg.allAttr[0] = 0
    getAllAttr(human, sendMsg.allAttr)

    if not isPowerChange then
        local powerMsg = Msg.lw.LW_CHECK_RANK_CHANGE
        powerMsg.type = RankDefine.BAOWU_RANK_TYPE
        powerMsg.otherInfo = 0
        Msg.send2world(powerMsg, human._id)
    end
    Msg.send(sendMsg, human.fd)
end
--发送宝物列表
function sendBaowuList(human)
    local sendMsg = Msg.gc.GC_BAOWU_LIST
    sendMsg.info[0] = #BaowuConfig
    for i=1, #BaowuConfig do
        setBaowuInfo(human, i, sendMsg.info[i])
    end
    sendMsg.allAttr[0] = 0
    getAllAttr(human, sendMsg.allAttr)
    Msg.send(sendMsg, human.fd)
    local powerMsg = Msg.lw.LW_CHECK_RANK_CHANGE
    powerMsg.type = RankDefine.BAOWU_RANK_TYPE
    powerMsg.otherInfo = 0
    Msg.send2world(powerMsg, human._id)
end
--激活宝物
function jihuo(human, id)
    local data = ObjHuman.getBaowu(human, id)
    if data and data.startTime == 0 then
      return
    end
    if BagLogic.getItemCount(human, BaowuConfig[id].needItemId) < BaowuConfig[id].needItemCnt then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BAOWU_JIHUO_ITEM_NOT_ENOUGH)
    end
    BagLogic.delItemsById(human, BaowuConfig[id].needItemId, BaowuConfig[id].needItemCnt, "baowu")
    if data then
      data.startTime = 0
      data.limitTime = 0
      data.status = 1
    else
      data = ObjHuman.addBaowu(human, id, BaowuConfig[id].skillId)
    end
    UpdateBaowu(human, id)
    ObjHuman.doCalc(human)
end
--激活限时宝物
function jihuoLimitBaowu(human, id, limitTime, itemId, itemCnt)
    local data = ObjHuman.getBaowu(human, id)
    if not data then
      data = ObjHuman.addBaowu(human, id, BaowuConfig[id].skillId)
    else
      if data.startTime == 0 then --已经永久激活
        if not BagLogic.checkCanAddItem(human, itemId, true, itemCnt) then
          return Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.BAG_FULL)
        else
          BagLogic.addItem(human, itemId, true, itemCnt, "baowu")
          return true
        end
      end
      data.status = 1
    end
    data.startTime = os.time()
    data.limitTime = limitTime 
    UpdateBaowu(human, id)   
    ObjHuman.doCalc(human)
    return true
end
--检查神将是否到期
function checkLimtBaowu(human)
    for i = 0, human.db.baowuLen -1 do
      local data = human.db.baowu[i]
      if data.startTime ~= 0 and data.limitTime ~= 0 and data.limitTime <= (os.time() - data.startTime + 10) then
        human.db.baowu[i].limitTime = 0          
        human.db.baowu[i].status = 0
        if data.id == human.db.combatBaowu then
          human.db.combatBaowu = 0
          Status.sendViewStatus(human, true)
          SkillNotify.GC_SKILL_LIST(human)
          SkillNotify.GC_SKILL_LIST_CHANGE(human)
          SkillNotify.GC_SKILL_MAIN_MENU(human)
        end
        UpdateBaowu(human, data.id)
        ObjHuman.doCalc(human)
      end
    end
end
--发送宝物升级所需道具
function sendNeedItem(human)
    local sendMsg = Msg.gc.GC_BAOWU_LV_UP_NEED_ITEM
    local cache = {}
    sendMsg.info[0] = #common
    for i=1, #common do
        Grid.create(cache, common[i].itemId, 1)
        Grid.makeItem(cache, sendMsg.info[i].item, 0, human)
        sendMsg.info[i].needLv = common[i].needLv
        sendMsg.info[i].addExp = common[i].addExp
    end
    Msg.send(sendMsg, human.fd)
end
function isRightItem(human, data, itemId)
    for i=1, #common do
        if common[i].itemId == itemId and (not common[i+1] or data.lv < common[i+1].needLv) then
            return i
        end
    end
    return false
end
--计算倍数
function calcMulti(human, index)
    local multiAdd = math.random(1, 10000)
    local multi = 1 --随机到几倍暴击
    for i=1, #common[index].critWeight do
        multi = multi + common[index].critWeight[i][2]
        if multiAdd <= multi then
            multi = common[index].critWeight[i][1]
            break
        end
    end
    return multi
end

function _addExp(human, id, exp, send)
    if not _checkStatus(human, id) then
        return false
    end
    local data = ObjHuman.getBaowu(human, id)
    if data then
        local BaowuLvConfig = BaowuExcel[BaowuConfig[id].name.."Lv"]
        if human.db.lv <= data.lv and data.exp == BaowuLvConfig[data.lv + 1].exp then
            Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BAOWU_LVUP_EXP_FULL)
            return false
        end
        data.exp = data.exp + exp 
        _checkLvUp(human, id)
        if send then
            UpdateBaowu(human, id)
        end
        return true
    end
    return false
end

function _addLv(human, id, lv, send)
    if not _checkStatus(human, id) then
        return
    end
    local data = ObjHuman.getBaowu(human, id)
    if data then
        local BaowuLvConfig = BaowuExcel[BaowuConfig[id].name.."Lv"]
        if human.db.lv > data.lv then
            data.lv = data.lv + 1
            if send then
                UpdateBaowu(human, id)
            end
            return true, data.lv
        elseif human.db.lv == data.lv and data.exp < BaowuLvConfig[data.lv + 1].exp then
            data.exp = BaowuLvConfig[data.lv + 1].exp
            if send then
                UpdateBaowu(human, id)
            end
            return true, data.lv
        else
            Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BAOWU_LVUP_EXP_FULL)
            return false, data.lv
        end
    end
    return false, data.lv
end

function _checkLvUp(human, id)
    local data = ObjHuman.getBaowu(human, id)
    if data then
        local nLvUp = 0
        local BaowuLvConfig = BaowuExcel[BaowuConfig[id].name.."Lv"]
        if BaowuLvConfig then
            while BaowuLvConfig[data.lv + 1] and data.exp >= BaowuLvConfig[data.lv + 1].exp do
                if data.exp >= BaowuLvConfig[data.lv + 1].exp and human.db.lv > data.lv then
                    data.exp = data.exp - BaowuLvConfig[data.lv + 1].exp
                    data.lv = data.lv + 1
                    ObjHuman.doCalc(human)
                    nLvUp = 1 
                elseif data.exp >= BaowuLvConfig[data.lv + 1].exp and human.db.lv == data.lv then
                    data.exp = BaowuLvConfig[data.lv + 1].exp
                    break
                end
            end
            return nLvUp == 1 and true or false, data.lv
        end
    end
    return false, 0
end

function _checkStatus(human, id, isCombat)
    if not BaowuConfig[id] then
        if isCombat then
            Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.ITEM_ERR_BAOWU_NOCOMBAT)
        end
        return false
    end

    local BaowuLvConfig = BaowuExcel[BaowuConfig[id].name.."Lv"]
    if not BaowuLvConfig then
        return false
    end

    local data = ObjHuman.getBaowu(human, id)
    if not data then
        return false
    end

    if not BaowuLvConfig[data.lv + 1] then
        return false
    end

    if human.db.lv == data.lv and data.exp == BaowuLvConfig[data.lv + 1].exp then
        Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BAOWU_LVUP_EXP_FULL)
        return false
    end

    return true
end

--增加宝物经验
function addBaowuExp(human, id, index, cnt, itemId)
    local data = ObjHuman.getBaowu(human, id)
    local BaowuLvConfig = BaowuExcel[BaowuConfig[id].name.."Lv"]
    if not _checkStatus(human, id) then
        return
    end
    if BagLogic.getItemCount(human, itemId) < 1 then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BAOWU_LVUP_ITEM_NOT_ENOUGH)
    end
    for i=1, cnt do
        if _addExp(human, id, common[index].addExp * calcMulti(human, index)) then
        else
            break
        end
        --    if data.exp >= BaowuLvConfig[data.lv + 1].exp and human.db.lv > data.lv then
        --      data.exp = data.exp - BaowuLvConfig[data.lv + 1].exp
        --      data.lv = data.lv + 1
        --      ObjHuman.doCalc(human)
        --    elseif data.exp >= BaowuLvConfig[data.lv + 1].exp and human.db.lv == data.lv then
        --      data.exp = BaowuLvConfig[data.lv + 1].exp
        --    end
    end
    BagLogic.delItemsById(human, itemId, 1, "baowu")
    OfficialLogic.checkOneCondition(human, "baowuLv")
end
--检查批量使用道具是否重复
function checkRepeat(msg, new)
    local isHave = false
    for i=1, msg[0] do
        if msg[i].multi == new then
            msg[i].times = msg[i].times + 1
            isHave = true
        end
    end
    if not isHave then
        msg[0] = msg[0] + 1
        msg[msg[0] ].multi = new
        msg[msg[0] ].times = 1
    end
end
--批量使用道具升级宝物
function multiLvUp(human, id, itemId, cnt)
    if not _checkStatus(human, id) then
        return
    end

    local data = ObjHuman.getBaowu(human, id)
    local index = isRightItem(human, data, itemId)
    if not index then
        return
    end

    local BaowuLvConfig = BaowuExcel[BaowuConfig[id].name.."Lv"]
    if human.db.lv == data.lv and data.exp == BaowuLvConfig[data.lv + 1].exp then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BAOWU_LVUP_EXP_FULL)
    end
    if data.lv >= #BaowuLvConfig then
        return
    end
    if BagLogic.getItemCount(human, itemId) < cnt then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BAOWU_LVUP_ITEM_NOT_ENOUGH)
    end
    local sendMsg = Msg.gc.GC_BAOWU_LV_UP_MULTI
    sendMsg.times[0] = 0
    local useCnt = 0
    local multi
    local totalExp = 0
    while cnt > 0 do
        local multi = calcMulti(human, index)
        checkRepeat(sendMsg.times, multi)
        totalExp = totalExp + common[index].addExp * multi
        if _addExp(human, id, common[index].addExp * multi) then
        else
            break
        end
        useCnt = useCnt + 1
        cnt = cnt - 1
    end
    sendMsg.totalExp = totalExp
    sendMsg.itemCnt = useCnt
    BagLogic.delItemsById(human, itemId, useCnt, "baowu")
    OfficialLogic.checkOneCondition(human, "baowuLv")
    UpdateBaowu(human, id)
    Msg.send(sendMsg, human.fd)
end
--单个道具升级宝物
function lvUp(human, id, itemId, cnt)
    local data = ObjHuman.getBaowu(human, id)
    if not data then
        return
    end
    local index = isRightItem(human, data, itemId)
    if not index then
        return
    end
    local BaowuLvConfig = BaowuExcel[BaowuConfig[id].name.."Lv"]
    if human.db.lv == data.lv and data.exp == BaowuLvConfig[data.lv + 1].exp then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BAOWU_LVUP_EXP_FULL)
    end
    if data.lv >= #BaowuLvConfig then
        return
    end
    OfficialLogic.checkOneCondition(human, "baowuLv")
    addBaowuExp(human, id, index, cnt, itemId)
    UpdateBaowu(human, id)
end
--宝物升星
function starUp(human, id)
    local data = ObjHuman.getBaowu(human, id)
    if not data then
        return
    end
    local BaowuStarConfig = BaowuExcel[BaowuConfig[id].name.."Star"]
    if #BaowuStarConfig == (data.starLv + 1)/8 then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BAOWU_STARUP_STAR_FULL)
    end
    local quality = math.floor(data.starLv / 8) + 1
    if BagLogic.getItemCount(human, BaowuConfig[id].needItemId) < BaowuStarConfig[quality].addAttr[data.starLv % 8 + 1][3] then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BAOWU_STARUP_ITEM_NOT_ENOUGH)
    end
    if ObjHuman.getYinliang(human) < BaowuStarConfig[quality].addAttr[data.starLv % 8 + 1][4] then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BAOWU_STARUP_YINLIANG_NOT_ENOUGH)
    end
    ObjHuman.decYinliang(human, BaowuStarConfig[quality].addAttr[data.starLv % 8 + 1][4], "baowu")
    BagLogic.delItemsById(human, BaowuConfig[id].needItemId, BaowuStarConfig[quality].addAttr[data.starLv % 8 + 1][3], "baowu")
    data.starLv = data.starLv + 1
    UpdateBaowu(human, id)
    ObjHuman.doCalc(human)
end
function setStatus(human, data)
    if data.status == 2 then
        data.status = 1
        if data.id == human.db.combatBaowu then
            human.db.combatBaowu = 0
        end
    elseif data.status == 1 then
        data.status = 2
        if not data.id then
            assert(nil)
        end
        human.db.combatBaowu = data.id
        CoolDown.doCoolDown(human, data.skillId)
    end
end
--切换宝物状态
function changeStatus(human, id)
    local data = ObjHuman.getBaowu(human, id)
    if not data then
        return
    end
    local combatData
    local combatId = human.db.combatBaowu
    if human.db.combatBaowu ~= 0 and human.db.combatBaowu ~= id then
        combatData = ObjHuman.getBaowu(human, human.db.combatBaowu)
        setStatus(human, combatData)
        UpdateBaowu(human, combatId)
    end
    setStatus(human, data)
    UpdateBaowu(human, id)
    Status.sendViewStatus(human, true)
    SkillNotify.GC_SKILL_LIST(human)
    SkillNotify.GC_SKILL_LIST_CHANGE(human)
    SkillNotify.GC_SKILL_MAIN_MENU(human)
    ObjHuman.doCalc(human)
end
--得到当前出站的宝物技能
function getBaowuSkill(human)
    local data = ObjHuman.getBaowu(human, human.db.combatBaowu)
    if not data then
        return
    end
    if data.skillId == 0 then
          data.skillId = BaowuConfig[data.id].skillId        
    end
    return data.skillId, (data.status ~= 0 and data.skillLv or 0)
end
--得到宝物技能等级
function getBaowuLv(human, skillId)
    for i=0, human.db.baowuLen - 1 do
        if human.db.baowu[i].skillId == skillId then
            return human.db.baowu[i].lv
        end
    end
    return 0
end
--技能升级
function onSkillUpGrade(human, id, skillId)
    local data = ObjHuman.getBaowu(human, id)
    data.skillLv = data.skillLv + 1
    UpdateBaowu(human, id)
    if id == human.db.combatBaowu then
        SkillNotify.GC_SKILL_LIST(human)
        SkillNotify.GC_SKILL_LIST_CHANGE(human)
        SkillNotify.GC_SKILL_MAIN_MENU(human)
    end
    ObjHuman.doCalc(human)
end
--得到宝物信息
function getBaowuInfo(human, id)
    return BaowuConfig[id].source, BaowuConfig[id].effect
end

function setBaowuShowInfo(info, id)
    local curConfig = BaowuConfig[id]
    info.id = id
    info.effect = curConfig.image
    info.attr[0] = #curConfig.addAttr
    for i=1, #curConfig.addAttr do
        info.attr[i].key = curConfig.addAttr[i][1]
        info.attr[i].value = curConfig.addAttr[i][2]
    end
    info.type = 4
    info.power = RoleAttr.calPowerProto(info.attr)
end

function onGetCombatBaoWuLV(human)
    local data = ObjHuman.getBaowu(human, human.db.combatBaowu)
    if data then
        return data.lv
    end
    return nil
end

function gongfeng(human, id)
    local data = ObjHuman.getBaowu(human, id)
    if not data then
      return
    end
    local quality = math.floor(data.starLv / 8) == 0 and 0 or math.floor(data.starLv / 8)
    if data.LonghunLv + 1 > math.floor(data.lv / 2) then
      local str = string.format(Lang.BAOWU_GONGFENG_LV_NOT_ENOUGH, (data.LonghunLv + 1) * 2)
      return Broadcast.sendBroadcast(human, Lang.DOWN, str)
    end
    if quality < 1 then
        return
    end
    local BaowuLHConfig = BaowuExcel[BaowuConfig[id].name.."LH"]
    for i=1, #BaowuLHConfig[data.LonghunLv + 1].needItem do
        if BagLogic.getItemCount(human, BaowuLHConfig[data.LonghunLv + 1].needItem[i][1]) < BaowuLHConfig[data.LonghunLv + 1].needItem[i][2] then
            return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BAOWU_GONGFENG_ITEM_NOT_ENOUGH)
        end
    end
    if ObjHuman.getYinliang(human) < BaowuLHConfig[data.LonghunLv + 1].needYinliang then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BAOWU_STARUP_YINLIANG_NOT_ENOUGH)
    end
    for i=1, #BaowuLHConfig[data.LonghunLv + 1].needItem do
        BagLogic.delItemsById(human, BaowuLHConfig[data.LonghunLv + 1].needItem[i][1], BaowuLHConfig[data.LonghunLv + 1].needItem[i][2], "baowu")    
    end
    ObjHuman.decYinliang(human, BaowuLHConfig[data.LonghunLv + 1].needYinliang, "baowu")
    data.LonghunLv = data.LonghunLv + 1
    Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BAOWU_GONGFENG_SUCCESS)
    UpdateBaowu(human, id)
    ObjHuman.doCalc(human)
end

function checkEnough(human, data, YL, item)
    if ObjHuman.getYinliang(human) < YL then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BAOWU_STARUP_YINLIANG_NOT_ENOUGH)
    end
    for i=1, #item do
      if BagLogic.getItemCount(human, item[i][1]) < item[i][2] then
        local str = string.format(Lang.BAOWU_ITEM_NOT_ENOUGH, ItemDefine.getValue(item[i][1], "name"))
        return Broadcast.sendBroadcast(human, Lang.DOWN, str)
      end
    end
    ObjHuman.decYinliang(human, YL, "baowu")
    for i=1, #item do
        BagLogic.delItemsById(human, item[i][1], item[i][2], "baowu")    
    end
    return true
end

function jiHuoSkill(human, id, type)
    local data = ObjHuman.getBaowu(human, id)
    if not data then
      return
    end
    if type == 1 then
      if not checkEnough(human, data, BaowuConfig[id].leftSkill[1][3], BaowuConfig[id].leftNeedItem[1]) then
        return
      end
      if data.LonghunLv < BaowuConfig[id].leftSkill[1][2] and BaowuConfig[id].leftSkill[1][1] ~= 0 then
        return
      end
      data.leftSkillId = BaowuConfig[id].leftSkill[1][1]
    else
      if not checkEnough(human, data, BaowuConfig[id].rightSkill[1][3], BaowuConfig[id].rightNeedItem[1]) then
        return
      end
      if data.LonghunLv < BaowuConfig[id].rightSkill[1][2] and BaowuConfig[id].rightSkill[1][1] ~= 0 then
        return
      end
      data.rightSkillId = BaowuConfig[id].rightSkill[1][1]
    end
    UpdateBaowu(human, id)
end

function upStageSkill(human, id, type)
    local data = ObjHuman.getBaowu(human, id)
    if not data then
      return
    end
    if type == 1 then
      if not checkEnough(human, data, BaowuConfig[id].leftSkill[2][3], BaowuConfig[id].leftNeedItem[2]) then
        return
      end
      if data.LonghunLv < BaowuConfig[id].leftSkill[2][2] and BaowuConfig[id].leftSkill[1][1] ~= data.leftSkillId then
        return
      end      
      data.leftSkillId = BaowuConfig[id].leftSkill[2][1]
    else
      if not checkEnough(human, data, BaowuConfig[id].rightSkill[2][3], BaowuConfig[id].rightNeedItem[2]) then
        return
      end
      if data.LonghunLv < BaowuConfig[id].rightSkill[2][2] and BaowuConfig[id].rightSkill[1][1] ~= data.rightSkillId  then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BAOWU_STARUP_YINLIANG_NOT_ENOUGH)
      end
      data.rightSkillId = BaowuConfig[id].rightSkill[2][1]
    end
    UpdateBaowu(human, id)
end

function useSkill(human, id, type, openOrClose)
    local data = ObjHuman.getBaowu(human, id)
    if not data then
      return
    end
    if type == 1 then
      if data.leftSkillId == 0 then
        return
      end
      if openOrClose == 1 then
        data.skillId = data.leftSkillId
      else
        data.skillId = BaowuConfig[id].skillId
      end
    else
      if data.rightSkillId == 0 then
        return
      end
      if openOrClose == 1 then
        data.skillId = data.rightSkillId
      else
        data.skillId = BaowuConfig[id].skillId
      end 
    end
    CoolDown.doCoolDown(human, data.skillId)
    if id == human.db.combatBaowu then
      SkillNotify.GC_SKILL_LIST(human)
      SkillNotify.GC_SKILL_LIST_CHANGE(human)
      SkillNotify.GC_SKILL_MAIN_MENU(human)
    end
    UpdateBaowu(human, id)
end

function getBaowuLvCount(human, lv)
    local count = 0
    for i=1, #BaowuConfig do
        local data = ObjHuman.getBaowu(human, i)
        if data and data.lv >= lv then
          count = count + 1
        end
    end
    return count
end